//
//  ACEMyPayWayViewController.m
//  ACE
//
//  Created by zhangtao on 2018/8/14.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "ACEMyPayWayViewController.h"
#import "ACEMyPayWayCell.h"
#import "ACEAddBankCardViewController.h"
@interface ACEMyPayWayViewController ()

@end

@implementation ACEMyPayWayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"支付方式";
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"ACEMyPayWayCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self initNavRightWithImage:@"添加"];
}

-(void)itemClick:(UIBarButtonItem *)item{
    //添加
    NSLog(@"添加银行卡");
    if (item.tag == rightItemClick) {
        
    }
    ACEAddBankCardViewController *add = [[ACEAddBankCardViewController alloc]init];
    [self.navigationController pushViewController:add animated:YES];
}

-(void)deleteCardWithIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"删除银行卡");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ACEWidth/3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 15;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
   UIView *foot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ACEWidth, 15)];
    foot.backgroundColor = [UIColor whiteColor];
    return foot;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ACEMyPayWayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    cell.colors =     @[(id)HEXCOLOR(0xF8D79F).CGColor,
//          (id)HEXCOLOR(0xFE68C2).CGColor];
    // Configure the cell...
    NSString *str = [NSString stringWithFormat:@"gradient%ld",indexPath.section%2];
    cell.gradientImgV.image = [UIImage imageNamed:str];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"...%ld...",indexPath.section);
    [self.navigationController popViewControllerAnimated:YES];
}


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

#pragma mark --before ios 11
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删 除";
}

-(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)){
    
    UIContextualAction *deleteRowAction =
    [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"删        除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        
        [self deleteCardWithIndexPath:indexPath];
        completionHandler (YES);
    }];
    
    
    
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    return config;
    
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
//}
//
//-(void)layoutSubviews{
//    [super layoutSubviews];
    
    //iOS11版本以上,自定义删除按钮高度方法:
    if (@available(iOS 11.0 ,*)) {
        
        for (UIView *subview in self.tableView.subviews)
        {
            if([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])
            {
                UIView *swipeActionPullView = subview;
                //1.0修改背景颜色
                swipeActionPullView.backgroundColor =  [UIColor whiteColor];
                //1.1修改背景圆角
                swipeActionPullView.layer.cornerRadius = 5.f;
                swipeActionPullView.layer.masksToBounds = YES;
                
                //2.0修改按钮-颜色
                UIButton *swipeActionStandardBtn = subview.subviews[0];
                if ([swipeActionStandardBtn isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                    
                    CGFloat swipeActionStandardBtnOX = swipeActionStandardBtn.frame.origin.x;
                    CGFloat swipeActionStandardBtnW  = swipeActionStandardBtn.frame.size.width;
                    swipeActionStandardBtn.frame = CGRectMake(swipeActionStandardBtnOX, 10, /*swipeActionStandardBtnW*/100, /*self.cellHeight - 10*/100);
                    //2.1修改按钮背景色
                    swipeActionStandardBtn.backgroundColor =  RGB(243, 89, 92, 1);
                    //2.2修改按钮背景圆角
                    swipeActionStandardBtn.layer.cornerRadius = 5.f;
                    swipeActionStandardBtn.layer.masksToBounds = YES;
                    swipeActionStandardBtn.titleLabel.font = [UIFont systemFontOfSize:17];
                    NSString *action = @"删除";
                    swipeActionStandardBtn.titleLabel.text = action;
                    //                    swipeActionStandardBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
                }
            }
        }
    }
    
}


@end
